package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.common.CustomException;
import com.example.demo.common.R;
import com.example.demo.common.UserContext;
import com.example.demo.dto.ShoppingDto;
import com.example.demo.entity.ShoppingCart;
import com.example.demo.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * date 2022年07月24日 15:22
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        // 根据用户的id查询购物车
        Long userId = UserContext.getUserId();
        if(userId == null){
            throw new CustomException("请先登陆后在查看");
        }
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.orderByDesc("create_time");
        List<ShoppingCart> cartList = shoppingCartService.list(wrapper);
        return R.success(cartList);
    }
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        // 首先查询在其用户的购物表中是否存在 如果存在则在此基础上添加数量即可 如不存在则添加
        // 获取用户的id
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        Long userId = UserContext.getUserId();
        if (userId == null) {
            throw new CustomException("请用户先进行登陆");
        }
        shoppingCart.setUserId(userId);
        wrapper.eq("user_id",userId);
        if(shoppingCart.getUserId() !=null){
            wrapper.eq("dish_id",shoppingCart.getDishId());
        }else {
            wrapper.eq("setmeal_id",shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingOne = shoppingCartService.getOne(wrapper);
        if(shoppingOne ==null){
            // 表示用户第一次添加购物车
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(new Date());
            shoppingCartService.save(shoppingCart);
            shoppingOne =shoppingCart;
        }else {
            // 表示用户在其购物车中已经添加
            shoppingOne.setNumber(shoppingOne.getNumber()+1);
            shoppingCartService.updateById(shoppingOne);
        }
        return R.success(shoppingOne);
    }
    @PostMapping("/sub")
    public void sub(@RequestBody ShoppingDto shoppingDto) {
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        if(StringUtils.isEmpty(shoppingDto.getDishId())){
            wrapper.eq("setmeal_id", shoppingDto.getSetmealId());
        }else {
            wrapper.eq("dish_id", shoppingDto.getDishId());
        }
        ShoppingCart shoppingCart = shoppingCartService.getOne(wrapper);
        if(shoppingCart.getNumber() == 0){
            return;
        }
        if(shoppingCart.getNumber() ==1){
            shoppingCartService.removeById(shoppingCart.getId());
        }else {
            shoppingCart.setNumber(shoppingCart.getNumber()-1);
            shoppingCartService.updateById(shoppingCart);
        }
    }
    @DeleteMapping("/clean")
    public R<String> clean() {
        Long userId = UserContext.getUserId();
        if (userId == null) {
            throw new CustomException("请先登陆在进行清空");
        }
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        shoppingCartService.remove(wrapper);
        return R.success("购物车清空成功");
    }
}
